package com.csg.web.config;

import com.csg.web.interceptor.CustomFilter;
import com.csg.web.interceptor.CustomInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * web配置文件
 *
 * @author george
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomInterceptor());
    }

    /**
     * 声明CustomFilter过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean customFilter() {

        FilterRegistrationBean<CustomFilter> bean = new FilterRegistrationBean<>();
        CustomFilter filter = new CustomFilter();
        bean.setFilter(filter);
        // 定义需要过滤的url地址
        List<String> urlPatterns = new ArrayList<>();
        urlPatterns.add("/*");
        bean.setUrlPatterns(urlPatterns);

        return bean;
    }
}
